package dataflowVariety{
import scala.collection.mutable.HashMap

class Term 
{
  //contain vars
   var vars  = HashMap[String,Double]() 
   
   //constant of term
   var constant:Double = 0
   
   def setConst(c:Double)
   {
     constant = c 
   }
   
   def addVar(name:String)
   {
      vars(name) = 1
   }
   
   def addNegVar(name:String)
   {
      vars(name) = -1
   }
   
   def setCoef(c:Double)
   {
     for(v<-vars) vars(v._1) *= c
     constant *= c
   }
   
   def chain(t:Term)
   {
     constant+=t.constant
     
     for (k<-t.vars)
     {
       //we already have this var name
       if (vars.isDefinedAt(k._1)) vars(k._1) += (k._2)
       else vars(k._1) = k._2
     }
   }
   
   def negchain(t:Term)
   {
     constant-=t.constant
     
     for (k<-t.vars)
     {
       //we already have this var name
       if (vars.isDefinedAt(k._1)) vars(k._1) -= (k._2)
       else vars(k._1) = k._2
     }
   }
   
   def addVirtual(c:Int)  
   {
     addNegVar("virtual"+ c.toString())
   }
   
   override def toString():String = 
   {
     if (vars.isEmpty) return null
     else
     {
       var s = "List((\"$\"," + constant +")"
       for(v<-vars)
       {
         s +=  ",(\""+ v._1 + "\"," + v._2 +")"
       }
        s += ")"
       return s
     }
   }
}
}